package com.sunny.design.prototype.deep;

import java.io.*;
import java.util.Date;

/**
 * @author Sunny
 * @date 2019/12/31 16:58
 */
public class QiTianDaSheng extends Monkey implements Cloneable, Serializable {
    public JinGuBang jinGuBang;

    public QiTianDaSheng() {
        this.birthday = new Date();
        this.jinGuBang = new JinGuBang();
    }

    @Override
    protected Object clone() throws CloneNotSupportedException {
        return this.deepClone();
    }

    /**
     * 深克隆
     */
    public Object deepClone() {
        try {
            ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream();
            ObjectOutputStream outputStream = new ObjectOutputStream(byteOutputStream);
            outputStream.writeObject(this);

            ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(byteOutputStream.toByteArray());
            ObjectInputStream inputStream = new ObjectInputStream(byteArrayInputStream);
            QiTianDaSheng object = (QiTianDaSheng) inputStream.readObject();
            object.setBirthday(new Date());
            return object;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
